package e_stream;

// ==========================================
// InputStreamEx4
// make : pwg
// date : 2001
// note : 키보드로 입력한 데이터 저장하기
//          2003 : 1.3 이상으로 컨버젼
// ==========================================

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardStreamEx4 extends Frame implements ActionListener {
	myWindowAdapter myW;

	Panel panelTop, panelBottom;
	Button saveFileButton;
	TextArea text;

	String directory;
	String file_name;

	public KeyboardStreamEx4() {
		setLayout(null);
		myW = new myWindowAdapter();
		addWindowListener(myW);

		panelTop = new Panel();
		panelTop.setBounds(0, 25, 400, 30);
		add(panelTop);

		panelBottom = new Panel(new BorderLayout());
		panelBottom.setBounds(20, 60, 600, 400);
		add(panelBottom);

		saveFileButton = new Button("SAVE");
		panelTop.add(saveFileButton);
		saveFileButton.addActionListener(this);

		text = new TextArea(550, 340);
		panelBottom.add(text);
	}

	public void saveFile() // 동일한 이름으로 저장
	{
		FileOutputStream output;
		OutputStreamWriter writer;
		BufferedWriter buffer;
		String str = null;
		file_name = "samp.txt";

		try {
			output = new FileOutputStream(file_name);
			writer = new OutputStreamWriter(output);
			buffer = new BufferedWriter(writer);

			str = text.getText();

			if (str == null)
				return; // TextArea에 아무런 내용이 없으면 종료
			buffer.write(str, 0, str.length());

			buffer.close();
			writer.close();
			output.close();
		} catch (Exception ex) {
		}
	}

	public void actionPerformed(ActionEvent ev) {
		if (ev.getSource() == saveFileButton)
			saveFile();
	}

	public static void main(String[] args) {
		KeyboardStreamEx4 f = new KeyboardStreamEx4();
		f.setSize(650, 480);
		f.setVisible(true);
	}

	class myWindowAdapter extends WindowAdapter {
		public void windowClosing(WindowEvent ev) {
			System.exit(0);
		}
	};

}